/*
 * @Author: your name
 * @Date: 2020-08-18 09:45:07
 * @LastEditTime: 2020-08-26 20:20:19
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /tools/statics/rand/rand.js
 */
let KEY = "rand";
function addItem() {
    let str = "<div class='pure-form " + KEY + "'>" +
        "<input type=text class='item item-" + KEY + "' \>" +
        "<button class='add-bnt pure-button bnt-" + KEY + "'>Ins.</button>" +
        "<button class='del-bnt pure-button bnt-" + KEY + "'>Del.</button>" +
        "</div>";
    return str;
}

function randItem(d) {
    let str = "<div class='pure-form rand-" + KEY + "'>" +
        "<input type=text readonly=readonly class=rand-item value=" + d + "\>" +
        "</div>";
    return str;
}

function bindAdd(item) {
    randInit();
    $(item).prev().off('keyup').on('keyup', randInit);
    $(item).on('click', function (e) {
        let new_item = $(this).parent().before(addItem());
        bindAdd($(this).parent().prev().children('.add-bnt'));
        bindDel($(this).parent().prev().children('.del-bnt'));
    });
}

function bindDel(item) {
    $(item).on('click', function (e) {
        if ($("." + KEY).length > 1) {
            $(this).parent().remove();
            randInit();
        }
    });
}

function getAll() {
    let texts = [];
    let text_all = $("." + KEY).children('.item');
    let text_len = text_all.length;
    for (let i = 0; i < text_len; i++) {
        texts.push($(text_all[i]).val().trim().replace(/\s/g, ""));
    }

    return texts;
}

function randAll(arr) {
    let ret = arr;
    for (let i = 0; i < arr.length; i++) {
        ret = arr.sort(function (a, b) { return Math.random() > .5 ? -1 : 1; })
    }
    return ret;
}

function showRand(arr) {
    $("#item-rand").empty();
    for (let d in arr) {
        $("#item-rand").append(randItem(arr[d]));
    }
}

function randInit() {
    $("#sug").val("");
    showRand(getAll());
}

function setCookie(cname, cvalue, exdays) {
    let d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    let expires = "expires=" + d.toGMTString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}

function getCookie(cname) {
    let name = cname + "=";
    let ca = document.cookie.split(';');
    for (let i = 0; i < ca.length; i++) {
        let c = ca[i].trim();
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return "";
}

$(document).ready(function () {
    let new_item = $("#item-add").append(addItem());
    bindAdd(".add-bnt");
    bindDel(".del-bnt");

    $("#process").on('click', function () {
        let after_rand = randAll(getAll());
        showRand(after_rand);
        $("#sug").val(after_rand[0]);
    });

    // $("#save").on('click', function () {
    //     let values = getAll().toString();        
    //     setCookie('jerry-tools-rand', values, 7);
    // });

    // $("#load").on('click', function () {
    //     let values = getCookie('jerry-tools-rand').split(',');
    //     for (let value in values){

    //     }
    // });
});